package com.example.tiantian.battery;

import com.example.tiantian.R;
import com.example.tiantian.battery.NewBatteryActivity.OnInformationListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ChargeFragment extends Fragment implements OnInformationListener {

    static final int COLOR_SAFE = Color.GREEN;
    static final int COLOR_WARNING = Color.YELLOW;
    static final int COLOR_DANGER = Color.RED;

    ImageView imgBatteryLevel;
    TextView txtBatteryLevel;

    TextView txtQ;
    TextView txtR;
    TextView txtS;

    TextView tvContent;

    Drawable active;
    Drawable deactive;

    BatteryReceiver receiver;
    IntentFilter filter;

    Bundle extras;

    NewBatteryActivity activity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        active = getResources().getDrawable(R.drawable.battery_bulb_active);
        deactive = getResources().getDrawable(R.drawable.battery_bulb_deactive);
        active.setBounds(0, 0, active.getMinimumWidth(), active.getMinimumHeight());
        deactive.setBounds(0, 0, deactive.getMinimumWidth(), deactive.getMinimumHeight());

        Log.d("TianTian", "charge create");

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (NewBatteryActivity) activity;
        this.activity.setOnInformationListener(this);
        Log.d("TianTian", "charge attach");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        activity.setOnInformationListener(null);
        Log.d("TianTian", "charge detach");
    }

    @SuppressLint("InflateParams")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_battery_charge, null);
        imgBatteryLevel = (ImageView) v.findViewById(R.id.battery_info_image);
        txtBatteryLevel = (TextView) v.findViewById(R.id.battery_info_level);

        txtQ = (TextView) v.findViewById(R.id.battery_charge_q);
        txtR = (TextView) v.findViewById(R.id.battery_charge_r);
        txtS = (TextView) v.findViewById(R.id.battery_charge_s);

        tvContent = (TextView) v.findViewById(R.id.tvContent);

        if (extras != null) {
            setState();
        }
        Log.d("TianTian", "charge createView");

        String notice = new StringBuffer().append("健康小巴士：\n   电量低于10%时打开电池维护，为您进行健康充电，建议每月进行一次健康充电，保养电池 ")
                .append("\n快速充电阶段：使用恒流进行快速充电，快速将电池充到80%，建议继续进行，将手机电池充满。\n循环充电阶段：电量即将充满的状态。\n涓流充电：保持充电状态。").toString();
        tvContent.setText(notice);
        return v;
    }

    public void setState() {
        int batteryLevel = extras.getInt(BatteryManager.EXTRA_LEVEL, 0);
        int batteryStateCharge = extras.getInt(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
        if (batteryStateCharge == BatteryManager.BATTERY_STATUS_CHARGING) {

            if (batteryLevel == 100) {
                imgBatteryLevel.setImageResource(R.drawable.bt100);
            } else if (batteryLevel >= 90) {
                imgBatteryLevel.setImageResource(R.drawable.bt90);
            } else if (batteryLevel >= 80) {
                imgBatteryLevel.setImageResource(R.drawable.bt80);
            } else if (batteryLevel >= 70) {
                imgBatteryLevel.setImageResource(R.drawable.bt70);
            } else if (batteryLevel >= 50) {
                imgBatteryLevel.setImageResource(R.drawable.bt50);
            } else if (batteryLevel >= 30) {
                imgBatteryLevel.setImageResource(R.drawable.bt30);
            } else if (batteryLevel >= 20) {
                imgBatteryLevel.setImageResource(R.drawable.bt20);
            } else if (batteryLevel >= 10) {
                imgBatteryLevel.setImageResource(R.drawable.bt10);
            } else {
                imgBatteryLevel.setImageResource(R.drawable.bt0);
            }

            txtBatteryLevel.setText(batteryLevel + "%");
            txtBatteryLevel.setTextColor(
                    batteryLevel >= 70 ? COLOR_SAFE : (batteryLevel >= 20) ? COLOR_WARNING : COLOR_DANGER);

            if (batteryLevel < 80) {
                txtQ.setCompoundDrawables(null, active, null, null);
                txtR.setCompoundDrawables(null, deactive, null, null);
                txtS.setCompoundDrawables(null, deactive, null, null);
            } else if (batteryLevel < 98) {
                txtQ.setCompoundDrawables(null, deactive, null, null);
                txtR.setCompoundDrawables(null, active, null, null);
                txtS.setCompoundDrawables(null, deactive, null, null);
            } else {
                txtQ.setCompoundDrawables(null, deactive, null, null);
                txtR.setCompoundDrawables(null, deactive, null, null);
                txtS.setCompoundDrawables(null, active, null, null);
            }

        } else {
            txtQ.setCompoundDrawables(null, deactive, null, null);
            txtR.setCompoundDrawables(null, deactive, null, null);
            txtS.setCompoundDrawables(null, deactive, null, null);
        }
    }

    @Override
    public void onGetInformation(Bundle extras) {
        this.extras = extras;
        if (txtBatteryLevel != null) {
            setState();
        }
    }
}
